package engine

import (
	"log"

	"aqielife.cn/spider/singleSpider/zhenai/downloader"
	"aqielife.cn/spider/singleSpider/zhenai/model"
)

func Run(seeds ...model.Request) {
	// request 对列
	var requests []model.Request
	for _, r := range seeds {
		requests = append(requests, r)
	}

	// 每个request 等待 Fetch 结束
	for len(requests) > 0 {
		r := requests[0]
		requests = requests[1:]

		// 打印爬取url地址
		log.Printf("Fetching %s", r.Url)
		body, err := downloader.Fetch(r.Url)
		if err != nil {
			log.Printf("Fetcher : error "+"fetching url %s: %v", r.Url, err)
			continue
		}
		parserResult := r.ParserFunc(body)
		requests = append(requests, parserResult.Requests...) // ... 代表所有 eg: parserResult.Requests[0]

		for _, item := range parserResult.Items {
			// 打印 item
			log.Printf("Got item %v", item)
		}
	}
}
